<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="org.edu.system.DBConnect" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.time.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.util.Calendar" %>
<%
	String tc_id = (String)(session.getAttribute("UserName"));
	String sql = "SELECT * FROM teacher WHERE tc_id = '" + tc_id + "'";
	System.out.println(sql);
	
	Connection con = DBConnect.getCon();
	Statement stmt = null;
	ResultSet rs = null;
	stmt = con.createStatement();
	rs = stmt.executeQuery(sql);//执行命令
	if (rs.next())// 如果记录集非空，表明有匹配的用户ID
	{
			//正常进行
	} else {
		// 否则登录失败
		// response.sendRedirect ( "MyJsp.jsp" );
		response.sendRedirect("login_failure.jsp");
	}
%>
<!DOCTYPE html>
<html>
<head>
<link href="./bootstrap/css/bootstrap.min.css" rel="stylesheet">
<meta charset="utf-8">
<title>教师教务管理系统 - 学生考勤</title>
</head>
<body style="width:900px; margin:0 auto;">
<br>
<input type="button" class="btn btn-primary btn-sm" value="历史考勤" onclick="javawwscript:location='tea_history.jsp?tc_id=<%=tc_id%>'"/>
<% 
	LocalDate date= LocalDate.now(); 
	String week = "0";
	if(date.getDayOfWeek().equals("MONDAY"))
		week = "1";
	else if(date.getDayOfWeek().equals("TUESDAY"))
		week = "2";
	else if(date.getDayOfWeek().equals("WEDNESDAY"))
		week = "3";
	else if(date.getDayOfWeek().equals("THURDAY"))
		week = "4";
	else if(date.getDayOfWeek().equals("FRIDAY"))
		week = "5";
	
	sql = "select lesson_id,course_id,time,week from lesson where tc_id = '"+tc_id+"'";
	stmt = con.createStatement();
	rs = stmt.executeQuery(sql);//执行命令
	if(rs.next())
	{
		if(rs.getString("week").equals("3"))
		{
			Calendar c = Calendar.getInstance();
			int hour = c.get(Calendar.HOUR_OF_DAY);
			int minute = c.get(Calendar.MINUTE);
			String hour_minute = hour + ":" + minute;

			SimpleDateFormat fmt = new SimpleDateFormat("HH:mm");
			java.util.Date course_date = fmt.parse(rs.getString("time"));
			java.util.Date now = fmt.parse(hour_minute);
			long c_time = (course_date.getTime()-now.getTime())/1000/60;
			
			if(c_time <= 10 && c_time > 0)
			{
				String lesson_id = rs.getString("lesson_id");
				sql = "select course_name from course where course_id = '" + rs.getString("course_id")+"'";
				stmt = con.createStatement();
				rs = stmt.executeQuery(sql);//执行命令
				if(rs.next())
				{	out.print("<br><br><br><br><br><br><center>");
					out.print("<h1>即将上课："+rs.getString("course_name")+"</h1><br>");
					out.print("<input type=\"button\" class=\"btn btn-success btn-lg\" value=\"开始考勤\" onclick=\"javawwscript:location='tea_kaoqin.jsp?lesson_id="+lesson_id+"'\"/>");
					out.print("<br><br><h5>( 注意：请不要重复考勤 )</h5><br>");
					out.print("</center>");
				}
				System.out.println("显示开始考勤");
			}else
			{
				out.print("<br><br><br><br><br><br><center>");
				out.print("<h1>暂时无课</h1>");
				out.print("</center>");
			}
		}else
		{
			out.print("<br><br><br><br><br><br><center>");
			out.print("<h1>今天没课</h1>");
			out.print("</center>");
			System.out.println("显示没课");
		}
	}
%>
</body>
</html>